﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VS.Web.Model;
using System.Web.Configuration;
using System.IO;

namespace VS.Web
{
    public static class FileHandler
    {

        public static void HandleFile(WebFile file)
        {
            string uploadDir = WebConfigurationManager.AppSettings["UploadDir"];

            if (!string.IsNullOrEmpty(uploadDir))
            {
                if (uploadDir.IndexOf("~/") == 0)
                    uploadDir = HttpContext.Current.Server.MapPath(uploadDir);

                if (uploadDir.LastIndexOf("/") == uploadDir.Length - 1)
                    uploadDir = uploadDir.Substring(0, uploadDir.Length - 1);

                string fullFileName = string.Format("{0}/{1}", uploadDir, file.FileName);

                if (File.Exists(fullFileName))
                {
                    string ext = fullFileName.Substring(fullFileName.LastIndexOf("."));
                    string fName = fullFileName.Substring(0, fullFileName.LastIndexOf("."));
                    fullFileName = string.Format("{0}{1} (copie)", fName, ext);
                }

                File.WriteAllBytes(fullFileName, file.FileContent);
            }
        }

    }

 
}